/**
 * 
 */
package com.demo2do.boyle.common.handler.impl;

import java.io.IOException;
import java.util.Map;

import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.demo2do.boyle.common.handler.FileUploadHandler;

/**
 * @author downpour
 *
 */
public class FileUploadHandlerImpl implements FileUploadHandler {
	
	private static Logger logger = LoggerFactory.getLogger(FileUploadHandlerImpl.class);
	
	private String url;
	
	/**
	 * @param url the url to set
	 */
	public void setUrl(String url) {
		this.url = url;
	}

	/* (non-Javadoc)
	 * @see com.demo2do.boyle.common.handler.FileUploadHandler#getUploadToken()
	 */
	@SuppressWarnings("unchecked")
	public String getUploadToken() {
		
		String uptoken = null;
		
		try {
			Response response = Jsoup.connect(url)
									 .timeout(10000)
									 .method(Method.GET)
									 .execute();
			if(response.statusCode() == 200) {
				Map<String, Object> mapResult = JSON.parseObject(response.body(), Map.class);
				uptoken = mapResult.get("uptoken").toString();
			}
		} catch (IOException e) {
			logger.error("getUpToken()" , e);
		}
		
		return uptoken;
	}

}
